package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.ChannelDao;
import com.hxkj.domain.po.Channel;
import com.hxkj.domain.enums.ChannelExceptionEnum;
import com.hxkj.service.ChannelService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ChannelServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 11:59
 * @Version 1.0
 */

@Service
@Slf4j
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    ChannelDao channelDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct saveChannel(Channel channel) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            //Mr.zheng:对比频道名称是否已存在
            if (channelDao.getChannelByName(channel.getChannelName(), channel.getSiteId()).size() != 0) {
                resultMessageStruct.setCode(ChannelExceptionEnum.CHANNEL_IS_EXIST.getCode());
                resultMessageStruct.setMessage(ChannelExceptionEnum.CHANNEL_IS_EXIST.getMsg());
                return resultMessageStruct;
            }
            //Mr.zheng:对比频道别名是否已存在
            if (channelDao.getChannelByTempletName(channel.getTempletName(), channel.getSiteId()).size() != 0) {
                resultMessageStruct.setCode(ChannelExceptionEnum.CHANNEL_TEMPLETNAME_IS_EXIST.getCode());
                resultMessageStruct.setMessage(ChannelExceptionEnum.CHANNEL_TEMPLETNAME_IS_EXIST.getMsg());
                return resultMessageStruct;
            }
            channelDao.saveChannel(channel);
            resultMessageStruct.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(ChannelExceptionEnum.CHANNEL_SAVE_ERROE.getCode());
            resultMessageStruct.setMessage(ChannelExceptionEnum.CHANNEL_SAVE_ERROE.getMsg());
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    public PageInfo<Channel> getChannelList(Channel channel, PageTo pageTo) {
        Page<Channel> page = PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
        PageInfo<Channel> pageInfo = new PageInfo<>();
        List<Channel> channelList;
        try {
            channelList = channelDao.getChannelList(channel);
            pageInfo = new PageInfo<>(page);
            pageInfo.setList(channelList);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签列表失败");
        }
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct removeChannel(Channel channel) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            //Mr.zheng:切割id字符串
            String[] channelIds = channel.getChannelIds().split(",");
            //Mr.zheng:遍历删除
            for (int i = 0; i < channelIds.length; i++) {
                channelDao.removeChannelById(Long.valueOf(channelIds[i]));
            }

            resultMessageStruct.setCode(200);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            resultMessageStruct.setCode(ChannelExceptionEnum.CHANNEL_DELETE_ERROE.getCode());
            resultMessageStruct.setMessage(ChannelExceptionEnum.CHANNEL_DELETE_ERROE.getMsg());
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct modifyChannel(Channel channel) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            Channel ch = channelDao.getChannelById(channel.getChannelId());
            //Mr.zheng:频道名称或别名都没有修改
            if (ch.getChannelName().equals(channel.getChannelName()) && ch.getTempletName().equals(channel.getTempletName())) {
                channelDao.modifyChannel(channel);
                resultMessageStruct.setCode(200);
                return resultMessageStruct;
            }
            //Mr.zheng:频道名称和别名都被修改
            if (!ch.getChannelName().equals(channel.getChannelName()) && !ch.getTempletName().equals(channel.getTempletName())) {
                if (channelDao.getChannelByName(channel.getChannelName(), channel.getSiteId()).size() == 0 &&
                        channelDao.getChannelByTempletName(channel.getTempletName(), channel.getSiteId()).size() == 0) {
                    channelDao.modifyChannel(channel);
                    resultMessageStruct.setCode(200);
                    return resultMessageStruct;
                }
            }
            //Mr.zheng:频道名称修改，别名没修改
            if (!ch.getChannelName().equals(channel.getChannelName()) && ch.getTempletName().equals(channel.getTempletName())) {
                if (channelDao.getChannelByName(channel.getChannelName(), channel.getSiteId()).size() == 0) {
                    channelDao.modifyChannel(channel);
                    resultMessageStruct.setCode(200);
                    return resultMessageStruct;
                }
            }
            //Mr.zheng:频道别名修改，名称没修改
            if (ch.getChannelName().equals(channel.getChannelName()) && !ch.getTempletName().equals(channel.getTempletName())) {
                if (channelDao.getChannelByTempletName(channel.getTempletName(), channel.getSiteId()).size() == 0) {
                    channelDao.modifyChannel(channel);
                    resultMessageStruct.setCode(200);
                    return resultMessageStruct;
                }
            }
            resultMessageStruct.setCode(ChannelExceptionEnum.CHANNEL_IS_EXIST.getCode());
            resultMessageStruct.setMessage(ChannelExceptionEnum.CHANNEL_IS_EXIST.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(ChannelExceptionEnum.CHANNEL_UPDATE_ERROE.getCode());
            resultMessageStruct.setMessage(ChannelExceptionEnum.CHANNEL_UPDATE_ERROE.getMsg());
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    public ResultDataStruct<Channel> getChannel(Channel channel) throws Exception {
        ResultDataStruct<Channel> resultDataStruct = new ResultDataStruct<>();
        try {
            Channel cha = channelDao.getChannelById(channel.getChannelId());
            resultDataStruct.setData(cha);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签列表失败");
        }
        return resultDataStruct;
    }

    @Override
    public ResultDataStruct<List<Channel>> getListBySiteId(Channel channel) throws Exception {
        ResultDataStruct<List<Channel>> result = new ResultDataStruct<>();
        try {
            List<Channel> channels = channelDao.getListBySiteId(channel);
            result.setData(channels);
            result.setCode(200);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    public ResultDataStruct<Channel> getChannelByTempletId(Channel channel) throws Exception {
        ResultDataStruct<Channel> resultDataStruct = new ResultDataStruct<>();
        try {
            List<Channel> channelList = channelDao.getChannelByTempletId(channel);
            if (channelList != null && channelList.size() != 0) {
                channel = channelList.get(0);
                resultDataStruct.setData(channel);
                resultDataStruct.setCode(200);
                return resultDataStruct;
            }
            resultDataStruct.setCode(401);
            resultDataStruct.setMessage("没有频道引用该模板");
            return resultDataStruct;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultDataStruct<List<Channel>> getAllChannel(Channel channel) {
        ResultDataStruct<List<Channel>> resultDataStruct = new ResultDataStruct<>();
        try {
            resultDataStruct.setData(channelDao.getChannelList(channel));
            resultDataStruct.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            resultDataStruct.setCode(null);
            return resultDataStruct;
        }
        resultDataStruct.setCode(200);
        return resultDataStruct;
    }

}
